﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SppdProjectHelper
{
    public partial class Form_FastReportPrinter : FormBase
    {
        public Form_FastReportPrinter()
        {
            InitializeComponent();
        }

        public Form_FastReportPrinter( IControlPad pad )
            :base(pad)
        {
            InitializeComponent();
        }

        public ProjectConfig ProjectConfig { set { _prj_cnf = value; } }

        private ProjectConfig _prj_cnf=null;
        FastReport.Report _report=null;

        private void Form_FastReportPrinter_Load(object sender, EventArgs e)
        {
            //foreach (Control ctl in (this.previewControl_main.Controls["bar1"] as ))
            //foreach (Control ctl in this.previewControl_main.Controls)
            //    MessageBox.Show(ctl.GetType().ToString());
            //this.previewControl_main.Controls[3].Visible = false;//隐藏工具栏
            //previewControl_main.ToolbarVisible = false;//隐藏工具栏
            //this.previewControl_main.Scale(new SizeF(556, 768));
            /*打开设计界面*/
            //report.Design();
            //this.previewControl_main.Zoom = (float)0.7;

            /*用数据填充报表*/
            FastReportDataFill();
            /*必须放在 report.Show()方法后面，使报表的大小和Page页的大小相同*/
            this.previewControl_main.ZoomPageWidth();
        }

        public static DataTable ToDataTable<T>(IEnumerable<T> collection)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (collection.Count() > 0)
            {
                for (int i = 0; i < collection.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }

        protected virtual void FastReportDataFill()
        {
            this.Size = new Size(400, 800);
            try
            {
                FastReport.Report report = new FastReport.Report();
                _report = report;
                //report.Load(@"E:\项目\金钻石\报表汇总\SO 销售单.frx");
                report.Load(Misc.GetAppPath()+ @"\Resources\rptr\022 成品校审单.frx");
                report.Preview = this.previewControl_main;

                report.SetParameterValue("PrjName", _prj_cnf.MainCfg.PrjName.Replace(",",""));
                report.SetParameterValue("CmpyCode", _prj_cnf.MainCfg.CmpyCode);
                report.SetParameterValue("PrjCode", _prj_cnf.MainCfg.PrjCode);
                report.SetParameterValue("State", _prj_cnf.MainCfg.State);
                report.SetParameterValue("Subject", _prj_cnf.MainCfg.Subject);
                report.SetParameterValue("VolName", _prj_cnf.MainCfg.VolName);

                string saveAsName = "022 " + _prj_cnf.MainCfg.PrjCode +" "+ _prj_cnf.MainCfg.PrjName.Replace(",", "");
                report.FileName = saveAsName;

                //DataTable dt = ToDataTable<DAOSaleProductLine>(LstSaleOrderLine);
                //dt.TableName = "FS_TDS_SaleOrder";
                //DataSet ds = new DataSet();
                //ds.Tables.Add(dt);
                //report.RegisterData(dt,"Table1");/*只使用DT存不进去数据，还不晓得为什么*/
                //report.RegisterData(ds);
                report.Prepare();
                report.ShowPrepared();
                //report.Show();
            }
            catch (Exception ex) { SysMisc.LogHelper.Error(typeof(Form_FastReportPrinter), ex); }
        }

        protected virtual void Menu_Item_Click( object sender, EventArgs e)
        {
            switch((sender as DevComponents.DotNetBar.ButtonItem).Text)
            {
                case "关闭":
                    this.Close();
                    break;
                case "打印":
                    previewControl_main.Print();
                    break;
            }
        }
    }
}
